package switchviz.recording;

import switchviz.core.ResourceManager;
import switchviz.core.view.View;

import switchviz.gui.component.PaddingPanel;
import switchviz.gui.component.SlidePanel;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;


public class ReplayStatePanel extends PaddingPanel implements ActionListener
{
    //~ Static fields/initializers -----------------------------------------------------------------

    /**
     *
     */
    private static final long serialVersionUID = -6950463033667720111L;

    //~ Instance fields ----------------------------------------------------------------------------

    private Replay _replay;
    private View _view;
    private SlidePanel _slpanel = null;
    private JButton _buttback = null;
    private JButton _buttforward = null;
    private JButton _buttfastback = null;
    private JButton _buttfastforward = null;
    private JButton _buttpause = null;
    private JButton _buttstop = null;
    private JButton _buttplay = null;

    //~ Constructors -------------------------------------------------------------------------------

    public ReplayStatePanel(Replay replay, View view)
    {
        _replay = replay;
        _view = view;

        initialize();
    }

    //~ Methods ------------------------------------------------------------------------------------

    private void initialize()
    {
        Dimension labelwidth = new Dimension(50, 20);

        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.setBorder(BorderFactory.createTitledBorder("Replay"));

        _slpanel = new SlidePanel("0:00", 0, _replay.getLength() / 1000, 0, labelwidth);
        _slpanel.getSlider().setPaintLabels(false);
        _slpanel.getSlider().setPaintTicks(false);
        _slpanel.getSlider().setEnabled(false);

        _buttback = new JButton(ResourceManager.getInstance().getImageIconBackward());
        _buttback.setActionCommand(ResourceManager.getInstance().getActionBackward());
        _buttback.addActionListener(this);

        _buttforward = new JButton(ResourceManager.getInstance().getImageIconForward());
        _buttforward.setActionCommand(ResourceManager.getInstance().getActionForward());
        _buttforward.addActionListener(this);

        _buttfastback = new JButton(ResourceManager.getInstance().getImageIconFastBackward());
        _buttfastback.setActionCommand(ResourceManager.getInstance().getActionFastBackward());
        _buttfastback.addActionListener(this);

        _buttfastforward = new JButton(ResourceManager.getInstance().getImageIconFastForward());
        _buttfastforward.setActionCommand(ResourceManager.getInstance().getActionFastForward());
        _buttfastforward.addActionListener(this);

        _buttpause = new JButton(ResourceManager.getInstance().getImageIconPause());
        _buttpause.setActionCommand(ResourceManager.getInstance().getActionPause());
        _buttpause.addActionListener(this);

        _buttstop = new JButton(ResourceManager.getInstance().getImageIconStop());
        _buttstop.setActionCommand(ResourceManager.getInstance().getActionStop());
        _buttstop.addActionListener(this);

        _buttplay = new JButton(ResourceManager.getInstance().getImageIconStart());
        _buttplay.setActionCommand(ResourceManager.getInstance().getActionStart());
        _buttplay.addActionListener(this);

        PaddingPanel statecontainer = new PaddingPanel();

        //statecontainer.setLayout(new BoxLayout(statecontainer, BoxLayout.PAGE_AXIS));
        statecontainer.setBorder(BorderFactory.createLoweredBevelBorder());
        statecontainer.add(_buttfastback);
        statecontainer.add(_buttback);
        statecontainer.add(_buttplay);
        statecontainer.add(_buttpause);
        statecontainer.add(_buttstop);
        statecontainer.add(_buttforward);
        statecontainer.add(_buttfastforward);

        this.add(_slpanel);
        this.add(statecontainer);

        _replay.start();
    }

    private String timeToString(long time)
    {
        String s = String.valueOf(time / 60000) + ":";

        long sec = (_replay.getPosition() / 1000) % 60;

        if (sec < 10)
        {
            s = s + "0";
        }

        s = s + sec;

        return s;
    }

    public void updateState()
    {
        _slpanel.setText(timeToString(_replay.getPosition()));
        _slpanel.getSlider().setValue(_replay.getPosition() / 1000);
    }

    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();

        if (command.equals(ResourceManager.getInstance().getActionBackward()))
        {
            _replay.jumpTo(Math.max(0, _replay.getPosition() - 10000));
        }
        else if (command.equals(ResourceManager.getInstance().getActionForward()))
        {
            _replay.jumpTo(Math.min(_replay.getLength(), _replay.getPosition() + 10000));
        }
        else if (command.equals(ResourceManager.getInstance().getActionFastBackward()))
        {
            _replay.jumpTo(Math.max(0, _replay.getPosition() - 60000));
        }
        else if (command.equals(ResourceManager.getInstance().getActionFastForward()))
        {
            _replay.jumpTo(Math.min(_replay.getLength(), _replay.getPosition() + 60000));
        }
        else if (command.equals(ResourceManager.getInstance().getActionPause()))
        {
            _replay.pause();
            _view.setFreeze(true);
        }
        else if (command.equals(ResourceManager.getInstance().getActionStop()))
        {
            _replay.stop();
        }
        else if (command.equals(ResourceManager.getInstance().getActionStart()))
        {
            _replay.start();
            _view.setFreeze(false);
        }
    }
}
